<?php
/**
 * 获取合同相关的发票和收款
 * Created by PhpStorm.
 * User: 刘抱
 * Date: 2018/11/6
 * Time: 14:31
 */

namespace Home\Services;


class ContractService extends BaseService
{

    /**
     * 获取合同表array数据
     * @param $field
     * @param $where
     * @return mixed
     */
    public static function getContractArray($field, $where)
    {
        $table = 'db_contract';
        $where['del'] = 1;
        $result = parent::getResult($table, $field, $where);
        return $result;
    }

    /**
     * 获取合同收款计划数据
     * @param $field
     * @param $where
     * @return mixed
     */
    public static function getContractBackArray($field, $where)
    {
        $table = 'db_contract_back';
        $where['del'] = 1;
        $result = parent::getResult($table, $field, $where);
        return $result;
    }

    /**
     * 获取发票的开票数据
     * @param $field
     * @param $where
     * @return mixed
     */
    public static function getContractInvoiceArray($field, $where)
    {
        $table = 'db_contract_invoice';
        $where['del'] = 1;
        $result = parent::getResult($table, $field, $where);
        return $result;
    }

    /**
     * 获取收款记录array
     * @param $field
     * @param $where
     * @return mixed
     */
    public static function getContractReceiptArray($field, $where)
    {
        $table = 'db_contract_receipt';
        $where['del'] = 1;
        $result = parent::getResult($table, $field, $where);
        return $result;
    }

    /**
     * 获取合同的已开票的金额总数
     * @param $field
     * @param $where
     * @return mixed
     */
    public static function getContractInvoice($field, $where)
    {
        $db_contract_invoice = M('db_contract_invoice');
        $where['del'] = 1;
        $result = $db_contract_invoice->field($field)->where($where)->group('contract_id')->select();
        return $result;
    }

    /**
     * 获取合同的收款总金额数
     * @param $field
     * @param $where
     * @return mixed
     */
    public static function getContractReceipt($field, $where)
    {
        $db_contract_receipt = M('db_contract_receipt');
        $where['del'] = 1;
        $result = $db_contract_receipt->field($field)->where($where)->group('contract_id')->select();
        return $result;
    }

    /**
     * 最后组成想得到的项目款项数据
     * @param $res_contract
     * @param $res_project
     * @param $res_contract_invoice
     * @param $res_contract_receipt
     * @return array
     */
    public static function lastResult($res_contract, $res_project, $res_contract_invoice, $res_contract_receipt, $back_status, $invoice_status)
    {
        $result = array();
        foreach ($res_contract as $contract_key => $contract) {
            foreach ($res_project as $project) {
                if ($contract['project_id'] == $project['project_id']) {
                    $result[$contract_key]['project_id'] = $project['project_id'];
                    $result[$contract_key]['project_num'] = $project['project_num'];
                    $result[$contract_key]['name'] = $project['name'];
                    $result[$contract_key]['type'] = $project['type'];
                    $result[$contract_key]['contract_id'] = $contract['contract_id'];
                    $result[$contract_key]['contract_money'] = $contract['contract_money'];
                    $result[$contract_key]['sign_contract_date'] = $contract['sign_contract_date'];
                }
            }
            foreach ($res_contract_invoice as $invoice) {
                if ($contract['contract_id'] == $invoice['contract_id']) {
                    $result[$contract_key]['make_money_sum'] = $invoice['make_money_sum'];
                    $result[$contract_key]['make_money_remain'] = $result[$contract_key]['contract_money'] - $invoice['make_money_sum'];
                }
            }
            foreach ($res_contract_receipt as $receipt) {
                if ($contract['contract_id'] == $receipt['contract_id']) {
                    $result[$contract_key]['money_sum'] = $receipt['money_sum'];
                    $result[$contract_key]['money_remain'] = $result[$contract_key]['contract_money'] - $receipt['money_sum'];
                }
            }
            if ($back_status == 1) {//已收
                if ($result[$contract_key]['money_remain'] != 0 || $result[$contract_key]['money_sum'] == 0) {
                    unset($result[$contract_key]);
                }
            } elseif ($back_status == 2) {//未收
                if ($result[$contract_key]['money_sum'] != 0) {
                    unset($result[$contract_key]);
                }
            } elseif ($back_status == 3) {//部分收
                if ($result[$contract_key]['money_remain'] == 0 || $result[$contract_key]['money_sum'] == 0) {
                    unset($result[$contract_key]);
                }
            }
            if ($invoice_status == 1) {//已开完发票
                if ($result[$contract_key]['contract_money'] - $result[$contract_key]['make_money_sum'] != 0) {
                    unset($result[$contract_key]);
                }
            } elseif ($invoice_status == 2) {//未开完发票
                if ($result[$contract_key]['contract_money'] - $result[$contract_key]['make_money_sum'] == 0) {
                    unset($result[$contract_key]);
                }
            }
        }
        return $result;
    }

    /**
     * 合计
     * @param $lastResult
     * @return array
     */
    public static function lastResultSum($lastResult)
    {
        $sum = array();
        foreach ($lastResult as $value) {
            $sum['contract_money_all'] += $value['contract_money'];
            $sum['make_money_sum_all'] += $value['make_money_sum'];
            $sum['make_money_remain_all'] += $value['make_money_remain'];
            $sum['money_sum_all'] += $value['money_sum'];
            $sum['money_remain_all'] += $value['money_remain'];
        }
        return $sum;
    }

    /**
     * 获取合同表单条数据
     * @param $field
     * @param $where
     * @return mixed
     */
    public static function getContract($field, $where)
    {
        $table = 'db_contract';
        $result = parent::getResultByField($table, $field, $where);
        return $result;
    }

    /**
     * 处理项目款项统计发票ajax结果
     * @param $res_contract_back
     * @param $res_contract_invoice
     * @return mixed
     */
    public static function do_invoice($res_contract_back, $res_contract_invoice)
    {
        foreach ($res_contract_invoice as $key=>$value){
            foreach ($res_contract_back as $item){
                if ($value['back_id'] == $item['back_id']) {
                    $res_contract_invoice[$key]['stage'] = $item['stage'];
                    $res_contract_invoice[$key]['stage_money'] = $item['stage_money'];
                    $res_contract_invoice[$key]['make_money_sum'] += $value['make_money'];
                    $res_contract_invoice[$key]['add_user'] = get_userName($value['add_uid']);
                }
            }
        }
        return $res_contract_invoice;
    }

    public static function do_receipt($res_contract_back,$res_contract_receipt)
    {
        foreach ($res_contract_receipt as $key=>$value){
            foreach ($res_contract_back as $item){
                if ($value['back_id'] == $item['back_id']) {
                    $res_contract_receipt[$key]['stage'] = $item['stage'];
                    $res_contract_receipt[$key]['stage_money'] = $item['stage_money'];
                    $res_contract_receipt[$key]['money_sum'] += $value['money'];
                    $res_contract_receipt[$key]['add_user'] = get_userName($value['add_uid']);
                }
            }
        }
        return $res_contract_receipt;
    }

}